home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / BORL_TIP / TI1000 / TI1726.ASC < prev    next >
Text File  |  1993-10-21  |  3KB  |  133 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland Pascal                        NUMBER  :  1726
  9.   VERSION  :  All
  10.        OS  :  All
  11.      DATE  :  October 21, 1993                         PAGE  :  1/2
  12.  
  13.     TITLE  :  Using procedural varialbles in Pascal
  14.  
  15.  
  16.  
  17.  
  18.     The included program, FUNC1.PAS shows how to use procedural
  19.     variables.
  20.  
  21.     It creates both a pointer to a function and a pointer
  22.     to a procedure and shows how to use them.
  23.  
  24.     Func1 creates a pointer to a function called MyFunc,
  25.     calls it once and then passes it to a procedure which
  26.     then proceeds to call it. Therefore this examples shows
  27.     how to assign and call a procedural variable, and
  28.     also how to pass it to another procedure which
  29.     can then call it.
  30.  
  31.     The following lines demonstrate how to create types
  32.     that can point at a procedure or function:
  33.  
  34.     type
  35.       TMyFunc = function(i: Integer): Integer;
  36.       TMyProc = procedure;
  37.  
  38.     Notice that the actual name of the function or procedure
  39.     is left out, but that it is necessary to declare any
  40.     variables passed to function or procedure, and to specify
  41.     any return types from a function.
  42.  
  43.     Its important to understand that TMyFunc and TMyProc are
  44.     really pointers to functions, even though there is no
  45.     need to call new and dispose, and no need to dereference
  46.     the pointer with the caret symbol ("^").
  47.  
  48.     Notice also that both MyProcedure and MyFunc are declared
  49.     "Far". This is absolutely necessary when working with
  50.     procedural variables, though of course functions declared
  51.     in the header to a unit will automatically be far when
  52.     called from another module.
  53.  
  54.  
  55.   program Func1;
  56.  
  57.   type
  58.     TMyFunc = function(i: Integer): Integer;
  59.     TMyProc = procedure;
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland Pascal                        NUMBER  :  1726
  75.   VERSION  :  All
  76.        OS  :  All
  77.      DATE  :  October 21, 1993                         PAGE  :  2/2
  78.  
  79.     TITLE  :  Using procedural varialbles in Pascal
  80.  
  81.  
  82.  
  83.  
  84.   procedure MyProcedure; far;
  85.   begin
  86.     WriteLn('Hi');
  87.   end;
  88.  
  89.   function MyFunc(I: Integer): Integer; far;
  90.   begin
  91.     WriteLn(i);
  92.   end;
  93.  
  94.   procedure Foo(F: TMyFunc);
  95.   begin
  96.     F(5);
  97.   end;
  98.  
  99.   var
  100.     F: TMyFunc;
  101.     P: TMyProc;
  102.   begin
  103.     F := MyFunc;
  104.     F(10);
  105.     Foo(F);
  106.     P := MyProcedure;
  107.     P;
  108.   end.
  109.  
  110.   DISCLAIMER: You have the right to use this technical information
  111.   subject to the terms of the No-Nonsense License Statement that
  112.   you received with the Borland product to which this information
  113.   pertains.
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.